<?php
defined('_JEXEC') or die('Restricted access');
require_once(JPATH_COMPONENT.DS.'persistencia'.DS.'DAOEvent.php');
require_once(JPATH_COMPONENT.DS.'persistencia'.DS.'tables'.DS.'evento.php');
require_once(JPATH_COMPONENT.DS.'dominio'.DS.'DTOFactory.php');

class JDAOEvent implements DAOEvent{

	public function saveEvent($evento){
		$factoria=DTOFactory::getInstance();
			
		$row=$factoria->fromDTOEvent2EventTable($evento);
					
		if(!$row->store()){
	        JError::raiseError(500, $row->getError());
	    }
	    $rowtmp=$this->singleAssociativeQuery("select e.idEvento,e.idCreador,e.idCategoria,e.idAreaAdministrativa, e.estado, e.nombreEvento, e.descripcionEvento, e.imagenEvento, e.coordenada, e.fecha, e.numvisitas, e.hora, u.name as nameCreator, c.nombreCategoria , a.nombreAreaAdministrativa from #__evento as e inner join #__users as u  ON e.idCreador=u.id inner join #__categoria as c ON e.idCategoria=c.idCategoria inner join #__areaadministrativa as a ON e.idAreaAdministrativa=a.idAreaAdministrativa WHERE e.idEvento=".$row->idEvento);
	    return $factoria->fromEventArray2DTOEvent($rowtmp);
	}
	
	public function updateEvent($evento)
	{
		return $this->saveEvent($evento);
	}
	
	public function readEventById($idEvent){
		$factoria = DTOFactory::getInstance();
		$row=$this->singleObjectQuery("SELECT * FROM #__evento WHERE idEvento=".$idEvent);
		return $factoria->fromEventTable2DTOEvent($row);
	}
	
	public function checkEventCreator($eventID, $userID) {
		
		$eventCreator = $this -> singleObjectQuery("SELECT idEvento FROM #__evento WHERE idEvento= ".$eventID." AND idCreador= ".$userID);	
		if ($eventCreator!=null)
			return true;
		else
			return false;
	}
	
	public function readEvents(){
		// sacamos la lista de eventos
		// la factoria la convierte en una lista de DTOEvent
		$row=$this->multipleRowQuery("select e.idEvento,e.idCreador,e.idCategoria,e.idAreaAdministrativa, e.estado, e.nombreEvento, e.descripcionEvento, e.imagenEvento, e.coordenada, e.fecha, e.numvisitas, e.hora, u.name as nameCreator, c.nombreCategoria , a.nombreAreaAdministrativa from #__evento as e inner join #__users as u  ON e.idCreador=u.id inner join #__categoria as c ON e.idCategoria=c.idCategoria inner join #__areaAdministrativa as a ON e.idAreaAdministrativa=a.idAreaAdministrativa");
		$factoria=DTOFactory::getInstance();
		$listaDtoEvent=$factoria->fromEventArrayList2DTOEventList($row);
		return $listaDtoEvent;
	}
	
	public function getEventViews($idEvent){
		$row=$this->singleObjectQuery("SELECT numVisitas FROM #__evento WHERE idEvento=".$idEvent);
		return $row->numVisitas;
	}
	
	public function addEventView($idEvent){
		$db =& JFactory::getDBO();
		$db->setQuery( "UPDATE #__evento SET numVisitas=numVisitas+1 WHERE idEvento=".$idEvent );
		if($db->query()){
			return true;
		}
		return false;
	}
	
	public function readUserCreatedEvents($idUser)
	{
		// sacamos la lista de eventos creados por un usuario 
		//$listaEventos = $this->multipleObjectQuery("SELECT * FROM #__evento WHERE idCreador = ".$idUser);
		$row=$this->multipleRowQuery("select e.idEvento,e.idCreador,e.idCategoria,e.idAreaAdministrativa, e.estado, e.nombreEvento, e.descripcionEvento, e.imagenEvento, e.coordenada, e.fecha, e.numvisitas, e.hora, u.name as nameCreator, c.nombreCategoria , a.nombreAreaAdministrativa from #__evento as e inner join #__users as u  ON e.idCreador=u.id inner join #__categoria as c ON e.idCategoria=c.idCategoria inner join #__areaadministrativa as a ON e.idAreaAdministrativa=a.idAreaAdministrativa WHERE e.idCreador = ".$idUser);
		// la factoria la convierte en una lista de DTOEvent
		$factoria=DTOFactory::getInstance();
		//$listaDtoEvent=$factoria->fromObjectList2DTOEventList($listaEventos);
		$listaDtoEvent=$factoria->fromEventArrayList2DTOEventList($row);
		
		return $listaDtoEvent;
	}	
	public function deleteEvent($idEvent){
		$factoria=DTOFactory::getInstance();
		$evento=$this->singleObjectQuery("SELECT * FROM #__evento WHERE idEvento = ".$idEvent);
		//print_r($evento);
		$row=$factoria->fromEventObject2Table($evento);
		$row->delete($evento->idEvento);
	}
	
	private function singleObjectQuery($query){
		$db =& JFactory::getDBO();
		$db->setQuery( $query );
		$row = $db->loadObject();
		if ($db->getErrorNum()) {
			echo $db->stderr();
			return false;
		}
		return $row;
	}
	private function multipleObjectQuery($query){
		$db =& JFactory::getDBO();
		$db->setQuery($query);
		return $db->loadObjectList();
	}
	private function multipleRowQuery($query){
		$db =& JFactory::getDBO();
		$db->setQuery($query);
		return $db->loadAssocList();
	}
	private function singleAssociativeQuery($query){
		$db =& JFactory::getDBO();
		$db->setQuery( $query );
		$row = $db->loadAssoc();
		if ($db->getErrorNum()) {
			echo $db->stderr();
			return false;
		}
		return $row;
	}
 
}

?>